<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Arr;

/**
 * 管理菜单数据填充器
 */
class AdminMenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     * php artisan db:seed --class=\\Database\\Seeders\\AdminMenuSeeder
     * or
     * php artisan db:seed --class=\Database\Seeders\AdminMenuSeeder
     * @return void
     */
    public function run()
    {
        $data = [
            [
                'title' => '首页',
                'icon' => 'fa-home',
                'uri' => '/',
            ],
            [
                'title' => '系统管理',
                'icon' => 'fa-tasks',
                'uri' => '',
                'children' => [
                    [
                        'title' => '管理员',
                        'icon' => 'fa-users',
                        'uri' => 'auth/users',
                    ],
                    [
                        'title' => '角色',
                        'icon' => 'fa-user',
                        'uri' => 'auth/roles',
                    ],
                    [
                        'title' => '权限',
                        'icon' => 'fa-ban',
                        'uri' => 'auth/permissions',
                    ],
                    [
                        'title' => '菜单',
                        'icon' => 'fa-bars',
                        'uri' => 'auth/menu',
                    ],
                    [
                        'title' => '操作日志',
                        'icon' => 'fa-history',
                        'uri' => 'auth/logs',
                    ],
                ],
            ],
            //以下菜单仅在开发环境有效
            [
                'title' => 'Helpers',
                'icon' => 'fa-gears',
                'uri' => '',
                'children' => [
                    [
                        'title' => 'Scaffold',
                        'icon' => 'fa-keyboard-o',
                        'uri' => 'helpers/scaffold',
                    ],
                    [
                        'title' => 'Database terminal',
                        'icon' => 'fa-database',
                        'uri' => 'helpers/terminal/database',
                    ],
                    [
                        'title' => 'Laravel artisan',
                        'icon' => 'fa-terminal',
                        'uri' => 'helpers/terminal/artisan',
                    ],
                    [
                        'title' => 'Routes',
                        'icon' => 'fa-list-alt',
                        'uri' => 'helpers/routes',
                    ],
                    [
                        'title' => 'Log viewer',
                        'icon' => 'fa-database',
                        'uri' => 'logs',
                    ],
                    [
                        'title' => 'Media manager',
                        'icon' => 'fa-file',
                        'uri' => 'media',
                    ],
                    [
                        'title' => 'Redis manager',
                        'icon' => 'fa-database',
                        'uri' => 'redis',
                    ],
                ],
            ],
        ];
        $class = config('admin.database.menu_model');
        $class::query()->truncate();
        $this->initData($data);
    }

    protected function initData($data = [], $parentId = 0)
    {
        $class = config('admin.database.menu_model');
        for ($i = 0; $i < count($data); $i++) {
            $insertData = Arr::only($data[$i], [
                'title',
                'icon',
                'uri',
            ]);
            $insertData['order'] = $i + 1;
            $insertData['parent_id'] = $parentId;
            $model = (new $class())->create($insertData);
            $children = Arr::get($data[$i], 'children', []);
            $this->initData($children, $model->id);
        }
    }
}
